﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Data;

namespace MS.InformationManagement.DAL.Support
{
    public static class QuerySiteHelper
    {

        public static SPSiteDataQuery getSiteQueryObject(List<string> listsId, List<string> viewFields, List<string> viewListFields, string queryFilter)
        {
            SPSiteDataQuery query = new SPSiteDataQuery();
            query.ViewFields = GetViewFields(viewFields, "FieldRef");
            query.ViewFields += GetViewFields(viewListFields, "ListProperty");
            query.Lists = GetListIds(listsId);
            query.Webs = "<Webs Scope='SiteCollection' />";
            query.Query = queryFilter;
            return query;
        }
        
        public static SPSiteDataQuery getSiteQueryObject(string listTemplateId, List<string> viewFields, string queryFilter)
        {
            SPSiteDataQuery query = new SPSiteDataQuery();
            query.ViewFields = GetViewFields(viewFields, "FieldRef");
            query.Lists = string.Format("<Lists ServerTemplate='{0}' MaxListLimit='100' />", listTemplateId);
            query.Webs = "<Webs Scope='SiteCollection' />";
            query.Query = queryFilter;
            return query;
        }

        public static SPSiteDataQuery getSiteQueryObject(string listTemplateId, List<string> viewFields, List<string> viewListFields, string queryFilter)
        {
            SPSiteDataQuery query = new SPSiteDataQuery();
            query.ViewFields = GetViewFields(viewFields, "FieldRef");
            query.ViewFields += GetViewFields(viewListFields, "ListProperty"); 
            query.Lists = string.Format("<Lists ServerTemplate='{0}' />", listTemplateId);
            query.Webs = "<Webs Scope='SiteCollection' />";
            query.Query = queryFilter;
            return query;
        }

        public static string GetViewFields(List<string> viewFields, string fieldType)
        {
            StringBuilder fields = new StringBuilder();
            foreach (var field in viewFields)
            {
                fields.Append(string.Format("<{0} Name='{1}' />", fieldType, field));
            }
            return fields.ToString();
        }

        public static string GetListIds(List<string> ListIds)
        {
            StringBuilder fields = new StringBuilder();
            fields.Append("<Lists>");
            foreach (var listId in ListIds)
            {
                fields.Append(string.Format("<List ID='{0}' />", listId));
            }
            fields.Append("</Lists>");
            return fields.ToString();
        }

        public static DataTable GetDataFromSiteWithElevatedPrivileges(string listTemplateId, List<string> viewFields, List<string> viewListFields, string queryFilter)
        { 
            DataTable results = new DataTable();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            { 
                using(SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using(SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        SPSiteDataQuery query = getSiteQueryObject(listTemplateId, viewFields, viewListFields, queryFilter);
                        results = currentWeb.GetSiteData(query);
                    }
                }
            });
            return results;
        }

    }
}
